package com.company.bookstore.service.impl;

import com.company.bookstore.common.formatUtils.FileUtil;
import com.company.bookstore.entities.book_module.Book;
import com.company.bookstore.entities.order_module.Order;
import com.company.bookstore.entities.user_admin_module.User;
import com.company.bookstore.service.dataExport_module.DataExportService;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Service
public class DataExportImpl implements DataExportService {

    @Override
    public void downloadBook(List<Book> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (Book book : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("图书ID", book.getId());
            map.put("书名", book.getBookName());
            map.put("作者", book.getAuthor());
            map.put("出版社", book.getPublisher());
            map.put("库存", book.getRepositoryNum());
            map.put("价格", book.getPrice());
            map.put("销量", book.getSales());
            if(book.getDeleted() == 0) {
                map.put("上架状态", "已上架");
            }
            else if(book.getDeleted() == 1){
                map.put("上架状态", "已下架");
            }
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public void downloadUser(List<User> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (User user : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("ID", user.getId());
            map.put("用户名", user.getUsername());
            map.put("邮箱", user.getEmail());
            map.put("手机号", user.getPhone());
            if(user.getGender()) {
                map.put("性别", "男");
            }
            else if(!user.getGender()){
                map.put("性别", "女");
            }
            if(user.getEnable() == 0) {
                map.put("状态", "禁用");
            }
            else if(user.getEnable() == 1){
                map.put("状态", "启用");
            }
            if(user.getDeleted() == 0) {
                map.put("是否删除", "否");
            }
            else if(user.getDeleted() == 1){
                map.put("是否删除", "是");
            }
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public void downloadOrder(List<Order> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (Order order : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("订单ID", order.getId());
            map.put("图书ID", order.getFkBookId());
            if(order.getBook() != null){
                map.put("图书名称", order.getBook().getBookName());
            }
            else {
                map.put("图书名称", "数据不存在");
            }
            map.put("用户ID", order.getFkUserId());
            if(order.getUser() != null){
                map.put("用户姓名", order.getUser().getUsername());
            }
            else {
                map.put("用户姓名", "用户不存在");
            }
            map.put("购买数量", order.getNum());
            map.put("单价", order.getPrice());
            map.put("总金额", order.getTotalPrice());
            if(order.getAddress() != null){
                map.put("地址", order.getAddress().getCountry() + order.getAddress().getCity()
                        + order.getAddress().getProvince() + order.getAddress().getTown() + order.getAddress().getDetail());
            }
            else {
                map.put("地址", "地址不存在");
            }
;
            map.put("创建时间", order.getCreateTime());
            map.put("完成时间", order.getFinishTime());
            if(order.getStatus() == 0) {
                map.put("订单状态", "下单未支付");
            }
            else if(order.getStatus() == 1){
                map.put("订单状态", "成功支付");
            }
            else if(order.getStatus() == 2){
                map.put("订单状态", "生成物流单号");
            }
            else if(order.getStatus() == 3){
                map.put("订单状态", "订单取消");
            }
            else if(order.getStatus() == 4){
                map.put("订单状态", "订单完成");
            }
            else if(order.getStatus() == 5){
                map.put("订单状态", "订单超时");
            }
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
